package com.newbingo.entity;

import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

import com.newbingo.domain.common.EntitySupport;
import com.newbingo.domain.common.Identity;

/**
 * 游戏玩法实体
 * @author ChansonYan
 *
 */
@Entity
public class GamePlay implements java.io.Serializable{
	@Identity
	private Integer Id;
	private String name;
	private String playDesc;
	private String playScript;
	private String scriptType;
	private Game game;
	private Set<BettingCell> bettingCells;  
	private GamePlayEngine playEngine;
	
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	public Integer getId() {
		return Id;
	}
	public void setId(Integer id) {
		Id = id;
	}
	/**
	 * 玩法名称
	 * @return
	 */
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	/**
	 * 玩法描述
	 * @return
	 */
	public String getPlayDesc() {
		return playDesc;
	}
	public void setPlayDesc(String playDesc) {
		this.playDesc = playDesc;
	}
	
	/**
	 * 玩法脚本代码
	 * @return
	 */
	public String getPlayScript() {
		return playScript;
	}
	public void setPlayScript(String playScript) {
		this.playScript = playScript;
	}
	
	/**
	 * 脚本所属语言类型
	 * @return
	 */
	public String getScriptType() {
		return scriptType;
	}
	public void setScriptType(String scriptType) {
		this.scriptType = scriptType;
	}
	
	/**
	 * 玩法所属的游戏，和Game构成多对一关系
	 * @return
	 */
	@ManyToOne(cascade={CascadeType.ALL})
	@JoinColumn(name="gameno")
	public Game getGame() {
		return game;
	}
	public void setGame(Game game) {
		this.game = game;
	}
	
	/**
	 * 投注格
	 */
	@OneToMany(mappedBy = "gamePlay", targetEntity = BettingCell.class, cascade = CascadeType.ALL,fetch=FetchType.LAZY)
	public Set<BettingCell> getBettingCells() {
		return bettingCells;
	}
	public void setBettingCells(Set<BettingCell> bettingCells) {
		this.bettingCells = bettingCells;
	}

	/**
	 * 判断投注格的玩法中奖情况
	 * @param lotteries
	 * @return
	 * @throws Exception
	 */
	public BettingCell getWinningCell(List<Lottery> lotteries) throws Exception {
		if (playEngine==null) {
			playEngine = new GamePlayEngine(scriptType, playScript);
		}
		Object v = playEngine.getWinningCell(lotteries);
		for (BettingCell bc : bettingCells) {
			if (bc.getName().equals(v)) return bc;
		}
		return null;
	}
}
 